var localImageTobase64=function (path) {
  return new Promise(function (resolve, reject) {
    wx.getFileSystemManager().readFile({
      filePath: path,
      encoding: "base64",
      success: function (res) {
        console.log(res)
        resolve(res.data);
      },
      fail: function (res) {
        console.log("fail===" + res)
        reject(res);
      }
    });
  });
}
var onTapSavePicture = function (data) {
  wx.showLoading({
    title: '正在保存中...', // 内容
  });
  getAuthorize("writePhotosAlbum").then(function (e) {
    var time = new Date().getTime()
    var fileSym = wx.getFileSystemManager()
    fileSym.writeFile({
      filePath: wx.env.USER_DATA_PATH + "/idphoto_" + time + ".png",
      data: data,
      encoding: "base64",
      success: function (e) {
        console.log("t===", JSON.stringify(e))
        wx.saveImageToPhotosAlbum({
          filePath: wx.env.USER_DATA_PATH + "/idphoto_" + time + ".png",
          success: function (res) {
            console.log("t==s=", JSON.stringify(e))
            wx.showModal({
              title: "保存成功",
              showCancel: true
            });
          },
          fail: function (res) {
            console.log("t==e1=", JSON.stringify(res))
            res.errMsg.includes("cancel") || wx.showModal({
              title: res.errMsg,
              showCancel: true
            });
          },
          complete: function () {
            wx.hideLoading();
          }
        });
      },
      fail: function (res) {
        console.log("t==e2=", JSON.stringify(res))
        wx.hideLoading();
      }
    });
  }).catch(function (res) {
    console.log("t==e1=c=", JSON.stringify(res))
    wx.hideLoading()
    wx.showModal({
      content: e.err,
      showCancel: true
    });
  });
}
var getAuthorize = function () {
  console.log("err=arguments==", JSON.stringify(arguments))
  var e = arguments[0];
  console.log("err=arguments=e=", JSON.stringify(e))
  return new Promise(function (resolve, reject) {
    wx.getSetting({
      success: function (res) {
        console.log("err=s==", JSON.stringify(res))
        res.authSetting.hasOwnProperty("scope." + e) && res.authSetting["scope." + e] ? resolve() : wx.authorize({
          scope: "scope." + e,
          success: function () {
            resolve()
          },
          fail: function (e) {
            console.log("err=e1==", JSON.stringify(e))
            reject(e)
          }
        });
      },
      fail: function (e) {
        console.log("err=e2==", JSON.stringify(e))
        reject(e)
      }
    });
  });
}
module.exports = {
  onTapSavePicture: onTapSavePicture,
  localImageTobase64: localImageTobase64,
}